package ftl.image;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.ParseException;
import android.os.Bundle;
import android.widget.ImageView;


public class TrabajoImagenesActivity extends Activity {
    /** Called when the activity is first created. */
	
	protected ImageView img;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.gorro);
        Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.imagen1);

        
        Bitmap bmOverlay = this.combineImages(bmp2,bmp1);
        
        img = (ImageView)findViewById(R.id.img1);
        
        img.setImageBitmap(bmOverlay);        
       


    }
    public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
        Bitmap cs = null; 
     
        int width, height = 0; 
        
        width = ((c.getWidth() > s.getWidth()) ? c.getWidth() : s.getWidth());
        height = ((c.getHeight() > s.getHeight()) ? c.getHeight() : s.getHeight());
        
        
        
        float x = ((c.getWidth() > s.getWidth()) ? (width / 2) - (s.getWidth()/2) : (width / 2) - (c.getWidth()/2));
        float y = ((c.getHeight() > s.getHeight()) ? (height / 2) - (s.getHeight()/2) : (height / 2) - (c.getHeight()/2));
        
//        
//        if(c.getWidth() > s.getWidth()) { 
//          width = c.getWidth(); 
//          height = c.getHeight() + s.getHeight(); 
//        } else { 
//          width = s.getWidth(); 
//          height = c.getHeight() + s.getHeight(); 
//        } 
     
        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     
        Canvas comboImage = new Canvas(cs); 
     
        comboImage.drawBitmap(c, 0f, 0f, null); 
        comboImage.drawBitmap(s, x, y, null); 
     
        // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
        /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 
     
        OutputStream os = null; 
        try { 
          os = new FileOutputStream(loc + tmpImg); 
          cs.compress(CompressFormat.PNG, 100, os); 
        } catch(IOException e) { 
          Log.e("combineImages", "problem combining images", e); 
        }*/ 
     
        return cs; 
      } 
}